//
//  GYNewCarView.m
//  GYDemo
//
//  Created by Mrzhang on 2018/6/26.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYNewCarView.h"
#import "Head.h"
#import "AttributeView.h"
@interface GYNewCarView ()<UITableViewDelegate,UITableViewDataSource,AttributeViewDelegate,UITextFieldDelegate>
{
    NSString *counts;
}
@property(nonatomic,strong)GCBaseTableView* mytable;
@property(nonatomic,strong)UIButton* finishBtn;
@property(nonatomic,strong)UIButton* cancelBtn;
@property(nonatomic,strong)AttributeView* attributeViewDS;
@property(nonatomic,strong)NSMutableArray* attrArr;
@end

@implementation GYNewCarView
-(NSMutableArray *)attrArr
{
    if (!_attrArr) {
        _attrArr=[[NSMutableArray alloc] init];
    }
    return _attrArr;
}
-(AttributeView *)attributeViewDS
{
    if (!_attributeViewDS)
    {
        NSArray *dsData = @[@"淘点评宝",@"京东",@"天点评网猫",@"亚马逊",@"大众点评网算电商吗",@"有货",@"有货",@"有货",@"有货",@"有货",@"有货",@"有货",@"有货",@"有货",@"有货"];
        
        _attributeViewDS=[AttributeView attributeViewWithTitle:@"产品种类" titleFont:SYSTEMFONT(15) attributeTexts:dsData viewWidth:WFCGFloatX(351)];
        _attributeViewDS.backgroundColor=[UIColor whiteColor];
        _attributeViewDS.Attribute_delegate=self;
        _attributeViewDS.y=0;
        _attributeViewDS.x=WFCGFloatX(13);
    }
    return _attributeViewDS;
}

-(UIButton *)finishBtn
{
    if (!_finishBtn) {
        _finishBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_finishBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_finishBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _finishBtn.titleLabel.font=SYSTEMFONT(15);
        _finishBtn.backgroundColor=Main_Color_Red;
        _finishBtn.frame=WFCGRectMake(0, 521-100, 375, 45);
        @WeakSelf;
        [_finishBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            
            if (![GYMineShopAddressViewModel shareInterface].defaultAddress)
            {
                [FTIndicator showErrorWithMessage:@"请填写收货地址!"];
            }else
            {
                NSDictionary *dic = @{@"num":counts};
                selfp.backHandler(2, dic);
            }

        }];
    }
    return _finishBtn;
}
-(UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        //_cancelBtn.backgroundColor=Main_Color_Red;
        _cancelBtn.frame=WFCGRectMake(348, 14, 13, 13);
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"share_cancel"] forState:UIControlStateNormal];
        [_cancelBtn setEnlargeEdge:30];
        @WeakSelf;
        [_cancelBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
            selfp.backHandler(3, nil);
        }];
    }
    return _cancelBtn;
}
-(GCBaseTableView *)mytable
{
    if (!_mytable) {
        
        _mytable=[[GCBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _mytable.frame=WFCGRectMake(0, 0, 375, 421);
        _mytable.dataSource=self;
        _mytable.delegate=self;
        [_mytable setSeparatorColor: Main_Color_BG];
        _mytable.showsVerticalScrollIndicator=NO;
    }
    return _mytable;
}
-(void)setViewModel:(GYMyBuyProductDetailViewModel *)viewModel
{
    if (viewModel) {
        _viewModel = viewModel;
//        NSMutableArray *marr = [NSMutableArray arrayWithArray:_viewModel.protypeArr];
//        for (int i=0; i<marr.count; i++)
//        {
//            AttributeView* cellAttributeViewDS=[AttributeView attributeViewWithTitle:@"产品种类" titleFont:SYSTEMFONT(15) attributeTexts:@[@"",@""] viewWidth:WFCGFloatX(351)];
//            cellAttributeViewDS.backgroundColor=[UIColor whiteColor];
//            cellAttributeViewDS.Attribute_delegate=self;
//            cellAttributeViewDS.y=0;
//            cellAttributeViewDS.x=WFCGFloatX(13);
//        }
//
//
//
//
//
//        [self.attributeViewDS setDatas:marr];
        [self.mytable reloadData];
    }
}
#pragma mark - life
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        counts = @"1";
        [self addSubview:self.mytable];
        [self addSubview:self.finishBtn];
        [self addSubview:self.cancelBtn];
        
    
    }
    return self;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row==0)
    {
        static NSString *identify = @"section1cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        [cell.contentView removeAllSubviews];
        
//        UIImageView* locationIV=[[UIImageView alloc] init];
//        locationIV.x=WFCGFloatX(87);
//        locationIV.y=WFCGFloatY(28);
//        locationIV.width=WFCGFloatX(203);
//        locationIV.height=locationIV.width;
//        locationIV.image=DefaultImage;
//        [cell.contentView addSubview:locationIV];
        
        SDCycleScrollView *_bannerScroll=[SDCycleScrollView cycleScrollViewWithFrame:WFCGRectMake(87, 28, 203, 203) delegate:nil placeholderImage:DefaultImage];
        _bannerScroll.backgroundColor=Main_Color_BGLightGray;
        _bannerScroll.currentPageDotColor=Main_Color_Red;
        _bannerScroll.pageDotColor=RGBACOLOR(216, 216, 216, 0.5);
        _bannerScroll.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
        if (_viewModel.BuyProductDetailInfoModel.imgpath)
        {
            _bannerScroll.imageURLStringsGroup=@[_viewModel.BuyProductDetailInfoModel.imgpath];
        }
        [cell.contentView addSubview:_bannerScroll];
        
        UILabel* moneyLab=[[UILabel alloc] init];
        moneyLab.x=WFCGFloatX(0);
        moneyLab.y=WFCGFloatY(240);
        moneyLab.width=ScreenWidth;
        moneyLab.height=WFCGFloatY(25);
        moneyLab.font=SYSTEMFONT(18);
        moneyLab.textColor=HEXCOLOR(@"#FC1B36");
        [cell.contentView addSubview:moneyLab];
        moneyLab.textAlignment=NSTextAlignmentCenter;
        NSString *prices = [NSString stringWithFormat:@"¥%.2ld",(long)_viewModel.BuyProductDetailInfoModel.price];
        moneyLab.text=prices;
        
        
        UILabel* titleLab=[[UILabel alloc] init];
        titleLab.x=WFCGFloatX(13);
        titleLab.y=WFCGFloatY(272);
        titleLab.width=WFCGFloatX(349);
        titleLab.height=WFCGFloatY(40);
        titleLab.font=SYSTEMFONT(14);
        titleLab.textColor=Main_Color_Black;
        [cell.contentView addSubview:titleLab];
        NSString *name = _viewModel.BuyProductDetailInfoModel.name;
        titleLab.text=name;
        
        return cell;
    }
    
    if (indexPath.row==1)
    {
        static NSString *identify = @"section4cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        [cell.contentView removeAllSubviews];
        
        
        UILabel* titleLab=[[UILabel alloc] init];
        titleLab.x=WFCGFloatX(13);
        titleLab.y=WFCGFloatY(0);
        titleLab.width=WFCGFloatX(100);
        titleLab.height=WFCGFloatY(50);
        titleLab.font=SYSTEMFONT(13);
        titleLab.textColor=HEXCOLOR(@"#666666");
        titleLab.text=@"购买数量";
        [cell.contentView addSubview:titleLab];
        
        
        UIButton* delBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [delBtn setTitle:@"-" forState:UIControlStateNormal];
        [delBtn setTitleColor:HEXCOLOR(@"#666666") forState:UIControlStateNormal];
        delBtn.titleLabel.font=SYSTEMFONT(18);
        delBtn.backgroundColor=HEXCOLOR(@"#F7F7F7");
        delBtn.tag=100;
        [cell.contentView addSubview:delBtn];
        
        delBtn.x=WFCGFloatX(244);
        delBtn.y=WFCGFloatX(15/2);
        delBtn.width=WFCGFloatX(35);
        delBtn.height=WFCGFloatX(35);
        
        
        
        UITextField* tf=[[UITextField alloc] init];
        tf.keyboardType=UIKeyboardTypeNumberPad;
        tf.textColor=Main_Color_Black;
        [cell.contentView addSubview:tf];
        tf.textAlignment=NSTextAlignmentCenter;
        tf.font=SYSTEMFONT(15);
        tf.text=counts;
        tf.delegate=self;
        tf.backgroundColor=HEXCOLOR(@"#F7F7F7");
        tf.tag=103;
        tf.x=WFCGFloatX(281);
        tf.y=WFCGFloatX(15/2);
        tf.width=WFCGFloatX(44);
        tf.height=WFCGFloatX(35);
        
        
        UIButton* addBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [addBtn setTitle:@"+" forState:UIControlStateNormal];
        [addBtn setTitleColor:HEXCOLOR(@"#666666") forState:UIControlStateNormal];
        addBtn.titleLabel.font=SYSTEMFONT(18);
        addBtn.backgroundColor=HEXCOLOR(@"#F7F7F7");
        [cell.contentView addSubview:addBtn];
        addBtn.tag=101;
        [addBtn addTarget:self action:@selector(BtnPress:) forControlEvents:UIControlEventTouchUpInside];
        [delBtn addTarget:self action:@selector(BtnPress:) forControlEvents:UIControlEventTouchUpInside];
        
        addBtn.x=WFCGFloatX(327);
        addBtn.y=WFCGFloatX(15/2);
        addBtn.width=WFCGFloatX(35);
        addBtn.height=addBtn.width;
        
        return cell;
    }
    if (indexPath.row==2)
    {
        static NSString *identify = @"section2cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        [cell.contentView removeAllSubviews];
        
        if ([GYMineShopAddressViewModel shareInterface].defaultAddress) {
            UILabel *titlelb = [UILabel new];
            titlelb.frame = WFCGRectMake(11, 10, 60, 19);
            titlelb.textColor = Main_Color_Gray;
            titlelb.font = SYSTEMFONT(13);
            titlelb.text = @"收货地址";
            [cell.contentView addSubview:titlelb];
            
            UILabel *contentlb = [UILabel new];
            contentlb.frame = WFCGRectMake(70, 10, 291, 39);
            contentlb.textColor = Main_Color_Black;
            contentlb.font = SYSTEMFONT(13);
            contentlb.numberOfLines = 0;
            NSString *province = [GYMineShopAddressViewModel shareInterface].defaultAddress.province;
            NSString *city = [GYMineShopAddressViewModel shareInterface].defaultAddress.city;
            NSString *area = [GYMineShopAddressViewModel shareInterface].defaultAddress.area;
            NSString *address = [GYMineShopAddressViewModel shareInterface].defaultAddress.addr;
            NSString *addresses = [NSString stringWithFormat:@"%@%@%@%@",province,city,area,address];
            contentlb.text = addresses;
            [cell.contentView addSubview:contentlb];
            titlelb.height = [WFFunctions WFStrGetSize:contentlb.text width:contentlb.width font:contentlb.font].height > WFCGFloatY(19) ? WFCGFloatY(39) : WFCGFloatY(19);
            contentlb.height = [WFFunctions WFStrGetSize:contentlb.text width:contentlb.width font:contentlb.font].height > WFCGFloatY(19) ? WFCGFloatY(39) : WFCGFloatY(19);
            @WeakSelf;
            [contentlb addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
                selfp.backHandler(1, nil);
            }];
            
        }else{
            UIButton* offerBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            [offerBtn setTitle:@"添加收货地址" forState:UIControlStateNormal];
            [offerBtn setTitleColor:HEXCOLOR(@"#333333") forState:UIControlStateNormal];
            offerBtn.titleLabel.font=SYSTEMFONT(12);
            [cell.contentView addSubview:offerBtn];
            offerBtn.backgroundColor=HEXCOLOR(@"#F7F7F7");
            
            offerBtn.x=WFCGFloatX(13);
            offerBtn.y=WFCGFloatY(20);
            offerBtn.width=WFCGFloatX(349);
            offerBtn.height=WFCGFloatY(36);
            [WFFunctions WFUIaddbordertoView:offerBtn radius:WFCGFloatY(18) width:0 color:[UIColor clearColor]];
            @WeakSelf;
            [offerBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
                selfp.backHandler(1, nil);
            }];
        }
        
        return cell;
    }
    
    static NSString *identify = @"section3cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    [cell.contentView removeAllSubviews];
    
   // [cell.contentView addSubview:self.attributeViewDS];
    
    AttributeView* cellAttributeViewDS=[AttributeView attributeViewWithTitle:@"产品种类" titleFont:SYSTEMFONT(15) attributeTexts:@[@"",@""] viewWidth:WFCGFloatX(351)];
    cellAttributeViewDS.backgroundColor=[UIColor whiteColor];
    cellAttributeViewDS.Attribute_delegate=self;
    cellAttributeViewDS.y=0;
    cellAttributeViewDS.x=WFCGFloatX(13);
    cellAttributeViewDS.tag=300;
    [cell.contentView addSubview:cellAttributeViewDS];
    
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)
    {
        return WFCGFloatY(312);
    }
    if (indexPath.row==1)
    {
        return WFCGFloatY(50);
    }
    if (indexPath.row==2)
    {
        return WFCGFloatY(80);
    }
   return WFCGFloatY(self.attributeViewDS.frame.size.height+10);
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0.001f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(void)layoutSubviews
{
    [super layoutSubviews];
}
-(void)Attribute_View:(AttributeView *)view didClickBtn:(UIButton *)btn
{
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField.text integerValue]<=1)
    {
        [FTIndicator showErrorWithMessage:@"商品数量不能小于1"];
        return;
    }
    counts = textField.text;
}
-(void)BtnPress:(UIButton* )sender
{
    UITableViewCell* cell=[_mytable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    UITextField* tf=(UITextField*)[cell.contentView viewWithTag:103];
    if (sender.tag==100)
    {
        //-
        if ([tf.text integerValue]<=1)
        {
            [FTIndicator showErrorWithMessage:@"商品数量不能小于1"];
            return;
        }else
        {
            tf.text=[NSString stringWithFormat:@"%ld",[tf.text integerValue]-1];
        }
        
    }else
    {
        //+
         tf.text=[NSString stringWithFormat:@"%ld",[tf.text integerValue]+1];
    }
    counts = tf.text;
}
@end
